﻿<UserControl x:Class="Airborne.ScrumPoker.Module.Developer.Views.CardsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:presentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"  
    xmlns:model="clr-namespace:Airborne.ScrumPoker.Model.Cards;assembly=Airborne.ScrumPoker"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="presentationOptions"
    Height="550" Width="Auto">
    <UserControl.Resources>

        <Storyboard x:Key="cardFlip">

            <ObjectAnimationUsingKeyFrames
                           Storyboard.TargetName="vp3D"
                           Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
                <DiscreteObjectKeyFrame KeyTime="0:0:1.1" Value="{x:Static Visibility.Hidden}" />
            </ObjectAnimationUsingKeyFrames>

            <ObjectAnimationUsingKeyFrames
                           Storyboard.TargetName="backWrapper"
                           Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static Visibility.Visible}"/>
            </ObjectAnimationUsingKeyFrames>

            <ObjectAnimationUsingKeyFrames
                           Storyboard.TargetName="frontWrapper"
                           Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.05" Value="{x:Static Visibility.Hidden}" />
            </ObjectAnimationUsingKeyFrames>


            <DoubleAnimation To="0" Duration="0:0:0.02"
                             Storyboard.TargetName="frontWrapper"
                             Storyboard.TargetProperty="Opacity" />

            <DoubleAnimation BeginTime="0:0:0.05" Duration="0:0:0.02" To="1"
                             Storyboard.TargetName="backWrapper"
                             Storyboard.TargetProperty="Opacity" />
        </Storyboard>

        <Style x:Key="PlayerCard" TargetType="{x:Type Border}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="BitmapEffect" >
                        <Setter.Value>
                            <OuterGlowBitmapEffect GlowColor="Yellow" GlowSize="9"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderBrush" Value="Black" />
                </Trigger>

                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="BorderBrush" Value="#FF434343" />
                </Trigger>

                <DataTrigger Binding="{Binding Path=IsSelected}" Value="True">
                    <Setter Property="BitmapEffect" >
                        <Setter.Value>
                            <OuterGlowBitmapEffect GlowColor="Cyan" GlowSize="9"/>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="BorderBrush" Value="Black" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <DataTemplate x:Key="CardDeck" DataType="{x:Type model:SimpleCard}" >
            <Border Background="White" Width="100" Height="136" CornerRadius="5,5,5,5" BorderBrush="Black" BorderThickness="1" >
                <Image Source="{Binding CardDeck}"
                   Name="cardImageDeck"  
                   Height="130" 
                   Width="100"
                   presentationOptions:Freeze="True"
                   RenderOptions.CachingHint="Cache"
                       RenderOptions.BitmapScalingMode="LowQuality"
                   />
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="CardFace">
            <Grid x:Name="LayoutRoot" Width="100" Height="136" >
                <Border Name="cardBorder" Background="White"  MouseDown="groupBoxCardSelected" CornerRadius="5,5,5,5" BorderThickness="1"  Style="{StaticResource PlayerCard}">
                    <Viewbox VerticalAlignment="Stretch" Height="Auto" Margin="10,10,10,10" >
                        <TextBlock   
                             Text="{Binding CardFaceValue}"
                             Background="White"
                             FontSize="30"
                             FontWeight="Bold"
                             Foreground="Black"
                             TextAlignment="Center"
                             />
                    </Viewbox>
                </Border>
                <Ellipse RenderTransformOrigin="0.51,0.536" Margin="2.364,31.986,-44.405,-75.567" Opacity="0.405" Clip="M138.27667,61.875415 C135.34107,56.998781 131.37767,51.871057 129.4113,49.735489 128.46743,48.710401 126.21949,48.365729 124.41317,49.802622 L50.78907,103.73588 50.198504,106.77547 98.527548,173.43822 C138.61437,151.60683 152.6015,87.013514 138.27667,61.875415 z">
                    <Ellipse.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="216.126"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Ellipse.RenderTransform>
                    <Ellipse.Fill>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFFFFFFF" Offset="0"/>
                            <GradientStop Color="#FFE0E0E0" Offset="1"/>
                        </LinearGradientBrush>
                    </Ellipse.Fill>
                </Ellipse>
            </Grid>
        </DataTemplate>

        <ControlTemplate x:Key="flipItemTemplate" TargetType="{x:Type GroupBox}">

            <Grid Width="100" Height="136">

                <Viewport3D Grid.Column="0" x:Name="vp3D" Visibility="Hidden">
                    <Viewport3D.Camera>
                        <PerspectiveCamera x:Name="camera" Position="0,0,0.5" LookDirection="0,0,-1" FieldOfView="90" />
                    </Viewport3D.Camera>

                    <Viewport3D.Children>

                        <ModelVisual3D>
                            <ModelVisual3D.Content>
                                <Model3DGroup>
                                    <DirectionalLight Color="#444" Direction="0,0,-1" />
                                    <AmbientLight Color="#BBB" />
                                </Model3DGroup>
                            </ModelVisual3D.Content>
                        </ModelVisual3D>
                        <ModelVisual3D>
                            <ModelVisual3D.Content>
                                <GeometryModel3D>
                                    <!-- Simple flat, square surface -->
                                    <GeometryModel3D.Geometry>
                                        <MeshGeometry3D
                                             TriangleIndices="0,1,2 2,3,0"
                                             TextureCoordinates="0,1 1,1 1,0 0,0"
                                             Positions="-0.5,-0.5,0 0.5,-0.5,0 0.5,0.5,0 -0.5,0.5,0" />
                                    </GeometryModel3D.Geometry>
                                    <GeometryModel3D.Material>
                                        <DiffuseMaterial>
                                            <DiffuseMaterial.Brush>
                                                <VisualBrush Visual="{Binding ElementName=frontHost}" />
                                            </DiffuseMaterial.Brush>
                                        </DiffuseMaterial>
                                    </GeometryModel3D.Material>
                                </GeometryModel3D>
                            </ModelVisual3D.Content>
                        </ModelVisual3D>
                    </Viewport3D.Children>

                </Viewport3D>


                <Border x:Name="frontWrapper">
                    <Border x:Name="frontHost" Background="Transparent">
                        <ContentPresenter  Content="{Binding}" ContentTemplate="{StaticResource CardDeck}" />
                    </Border>
                </Border>


                <Border x:Name="backWrapper" Grid.Column="0"  Visibility="Hidden" Opacity="0">
                    <Border x:Name="backHost" Background="Transparent">
                        <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource CardFace}" />
                    </Border>
                </Border>


            </Grid>

            <ControlTemplate.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource cardFlip}" />
                    </Trigger.EnterActions>
                </Trigger>
            </ControlTemplate.Triggers>

        </ControlTemplate>

    </UserControl.Resources>
    <Canvas Name="cardDeck" Height="Auto" Width="Auto" />
</UserControl>
